<?php
namespace Home\Controller;
use Think\Controller;
class IntegralController extends  PublicController
{
	
    private $common=array("update","xianjin");
    /**
     * 检测权限
     */
    private function checkright($isajax=false){
        $power=CONTROLLER_NAME."-".ACTION_NAME;
        $info=$_SESSION['my_info'];
        $bigcategroy=$info["bigcate"];
        $smallcategroy=$info["smallcate"];
        if(in_array(ACTION_NAME,$this->common)){
          
            if(!in_array($power, $smallcategroy)){
                if($isajax){
                    Message::Outofmessagenodata(99, "您没有权限");
                }else{
                    $this->error("没有权限");
                    exit();
                }
            }
        }
    }
	/**
	 * 兑换现金list
	 */
	public function dhxj(){
		
		$id=I("get.type",0);
		if($id=='0'||$id==0){
			$where=array('md_client.status'=>1,'md_zfb.biao'=>1);
		
		}else{
			$where=array('md_client.status'=>1,'md_zfb.type'=>$id,'md_zfb.biao'=>1);
		
		}
		$model=M("client");
		$count=$model->join('md_zfb ON md_client.uid = md_zfb.cid')->where($where)->count();
		$Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=$model->join('md_zfb ON md_client.uid = md_zfb.cid')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("time desc")->select();
// 		foreach($list as $k => $v){
// 			$ip = $v['ip']; 
// 		    $dz = $this->GetIpLookup($ip);		   
// 			$list[$k]['ip'] =$dz['ip'].$dz['city'];	
// 		}

		$this->assign("page",$show);
		$this->assign("list",$list);
		$this->display();
	}
	
	
	/**
	 * 兑换Q币list
	 */
	public function dhqb(){
		$id=I("get.type",0);
		if($id=='0'||$id==0){
			$where=array('md_client.status'=>1,'md_inte.biao'=>2);
		
		}else{
			$where=array('md_client.status'=>1,'md_inte.type'=>$id,'md_inte.biao'=>2);
		
		}
		$model=M("client");
		$count=$model->join('md_inte ON md_client.uid = md_inte.cid')->where($where)->count();
		$Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=$model->join('md_inte ON md_client.uid = md_inte.cid')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
// 		foreach($list as $k => $v){
// 			$ip = $v['ip'];
// 			$dz = $this->GetIpLookup($ip);
// 			$list[$k]['ip'] =$dz['ip'].$dz['city'];
// 		}
		
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->display();
		
	}
	
	/**
	 * 兑换话费list
	 */
	public function dhhf(){
		$id=I("get.type",0);
		if($id=='0'||$id==0){
			$where=array('md_client.status'=>1,'md_inte.biao'=>3);
		
		}else{
			$where=array('md_client.status'=>1,'md_inte.type'=>$id,'md_inte.biao'=>3);
		
		}
		$model=M("client");
		$count=$model->join('md_inte ON md_client.uid = md_inte.cid')->where($where)->count();
		$Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=$model->join('md_inte ON md_client.uid = md_inte.cid')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
// 		foreach($list as $k => $v){
// 			$ip = $v['ip'];
// 			$dz = $this->GetIpLookup($ip);
// 			$list[$k]['ip'] =$dz['ip'].$dz['city'];
// 		}
		
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->display();
	}
	
	/**
	 * 积分明细list
	 */
	public function jfmx(){
		
		$model=M("client");
		$count=$model->join('md_inte ON md_client.uid = md_inte.cid')->where(array('md_client.status'=>1))->count();
		$Page       = new \Think\Page($count,$this->pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=$model->join('md_inte ON md_client.uid = md_inte.cid')->where(array('md_client.status'=>1))->limit($Page->firstRow.','.$Page->listRows)->order('md_inte.times desc')->select();
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->display();
		$this->display();
	}
	
	
    

    
    
    //兑换处理
    public function update(){
        $this->checkright();
    	if(I('get.id')){ 
    		$cid=I('get.id');
    		$data['status'] = 0;
    		$ll = M('integral')->where(array('cid'=>$cid))->find();
    		
    		$data['score'] =$ll['score'] - $ll['dangscore']; //剩余的积分
    		$data['dangscore'] = 0; //清零申请兑换的积分
    		
    		$da['yuscore'] =$data['score']; //剩余的积分
    		$da['dangscore'] =$ll['dangscore'];//兑换的积分
    		$da['score'] =$ll['score']; //兑换前积分
    		$da['status'] =$ll['status'];//兑换现金/Q币/话费
    		$da['times']=date('Y-m-d H:i:s',time());
    		$da['cid'] =$cid;
    		$sa = M('integral')->where(array('cid'=>$cid))->save($data);
    		$aa = M('integraljilu')->add($da);
    		if($sa && $aa){
    			$this->success('处理成功！',U('integral/out'));
    		}else{
    			$this->error('处理失败！',U('integral/out'));
    		}
    	}
    }
    
    
    //充值
//     public function chong(){
//     	$id=I('get.id');
//     	$biao=I('get.biao');
//     	$type=I('get.type');
//     	$list=M('inte')->where(array('id'=>$id))->find();
//         $chong = $list['dhscore'];
//         //兑换数据信息
//         $xin = json_decode($list['mark'],true);///////////还未传过来
//         //接入久久
//         $_99dou = new \Org\Net\_99douInterface();       
//         $out_trade_id = uniqid(mt_rand(), true);       
//         $msg = "";      
//      exit; 
     
//         if($biao == '3' && $type == '1'){
//         	//手机充值
//         	$result = $_99dou->Direct($out_trade_id,'11213', 1,"18664045074","",$_SERVER["REMOTE_ADDR"],5,$msg);
//         	if($result == 0){
//         		$data['type']='2';
//         		$arr =M('inte')->where(array('id'=>$id))->save($data);
//         		if($arr){
//         			$this->success('充值成功！',U('Integral/dhhf'));
//         		}else{
//         			$this->error('充值成功,状态更改失败！',U('Integral/dhhf'));
//         		}       		
//         	}else{
//         		$this->error('充值失败！',U('Integral/dhhf'));
//         	}
//         }elseif($biao == '2' && $type == '1'){  
//         	//充值QB
//         	$result = $_99dou->Direct($out_trade_id,'10316', 1,"342698668","",$_SERVER["REMOTE_ADDR"],5,$msg);
//         	if($result == 0){
//         		$data['type']='2';
//         		$arr =M('inte')->where(array('id'=>$id))->save($data);
//         		if($arr){
//         			$this->success('充值成功！',U('Integral/dhqb'));
//         		}else{
//         			$this->error('充值成功,状态更改失败！',U('Integral/dhqb'));
//         		}        		
//         	}else{
//         		$this->error('充值失败！',U('Integral/dhqb'));
//         	}
//         }
        
//         /*   return 0 成功  1 失败  -1 未知
//         echo "直充返回:".$result." 消息：".$msg."<br/>";       
//         $success_qty=0;
//         $fail_qty=0;      
//         $result = $_99dou->Query($out_trade_id,$success_qty,$fail_qty,$msg);       
//         echo "查询返回:".$result." ;消息：".$msg.";success_qty:".$success_qty.";fail_qty:".$fail_qty;
//         */
//     }
    
    
    //现金兑换后更改状态
    public function xianjin(){
        $this->checkright();
    	$id=I('get.id');
    	$biao=I('get.biao');
    	$type=I('get.type');
    	$score=I('get.score');//前台加提醒，积分不够不能换
    	$scor =M('client')->where(array('uid'=>$id))->find();
    	if($scor['score'] < $score){
    		$this->error('积分不足，不能兑换！',U('Integral/dhxj'));
    	}else{
    		$da['score']=$scor['score']-$score;
    		M('client')->where(array('openid'=>$openid))->save($da);
    		if($biao == '1'){
    			$data['type']='2';
    			$arr =M('inte')->where(array('id'=>$id))->save($data);
    			if($arr){
    				$this->success('提交成功！',U('Integral/dhxj'));
    			}else{
    				$this->error('提交失败！',U('Integral/dhxj'));
    			}
    		}
    	}
    	
    }
    
    
    
    
    
    
}


?>